<template>
    <div>
        <head-top></head-top>

        <div class="popularAct table_container">

            <el-form :inline="true" :model="formAct" class="demo-form-inline" ref="formAct">
                <el-form-item label="活动类型" prop="activity_type">
                    <el-input v-model="formAct.activity_type" placeholder="请输入活动类型"></el-input>
                </el-form-item>
                <el-form-item label="是否过期" prop="expire">
                    <el-radio-group v-model="formAct.expire">
                        <el-radio label="全部"></el-radio>
                        <el-radio label="是"></el-radio>
                        <el-radio label="否"></el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>

            <div class="but-group">
                <el-button  type="primary" @click="addact">新增</el-button>
                <el-button  type="primary">搜索</el-button>
                <el-button  type="primary" @click="resetForm('formAct')">重置</el-button>
            </div>

            <el-table
                :data="tableData"
                style="width: 100%;margin-top: 20px">
                <el-table-column
                    prop="id"
                    label="序号"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="activity_type"
                    label="活动类型"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="expire"
                    label="是否过期">
                </el-table-column>
                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="modifyact(scope.$index, scope.row)">修改</el-button>
                        <el-button type="text" size="small" @click="handleDelete(scope.$index, scope.row, tableData)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="Pagination" style="text-align: left;margin-top: 10px;">
                <el-pagination
                    :current-page="currentPage"
                    :page-size="20"
                    layout="total, prev, pager, next"
                    :total="count">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title1" :visible.sync="dialogFormVisible">
            <el-form :model="form1":rules="rulesform1" ref="form1" label-width="80px">
                <el-form-item label="活动类型" prop="activity_type">
                    <el-input v-model="form1.activity_type" placeholder="请输入活动类型"></el-input>
                </el-form-item>
                <el-form-item label="有效期" required>
                    <el-col :span="11">
                        <el-form-item prop="start_time_str">
                        <el-date-picker type="date" placeholder="选择日期" v-model="form1.start_time_str" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col class="line" :span="2">至</el-col>
                    <el-col :span="11">
                        <el-form-item prop="end_time_str">
                            <el-date-picker type="date" placeholder="选择时间" v-model="form1.end_time_str" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="链接地址" prop="activity_link">
                    <el-input v-model="form1.activity_link" placeholder="请输入链接地址"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="activity_pic_url">
                    <el-upload
                        v-model="form1.activity_pic_url"
                        class="upload-demo"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :before-remove="beforeRemove"
                        multiple
                        :limit="3"
                        :file-list="fileList">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="resetForm('form1')">重置</el-button>
                <el-button type="primary">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import headTop from '@/components/headTop'
    export default {
        data() {
            return {
                rulesform1: {
                    activity_type: [
                        { required: true, message: '活动类型不能为空', trigger: 'blur' }
                    ],
                    start_time_str: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    end_time_str: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    activity_link: [
                        { required: true, message: '链接不能为空', trigger: 'blur' }
                    ],
                    activity_pic_url: [
                        { required: true, message: '请选择图片', trigger: 'change' }
                    ]
                },
                formAct: {
                    activity_type: '',
                    expire: '全部'
                },
                tableData: [
                    {
                        id: 1,
                        activity_type: '热推活动',
                        start_time: '1519833600000',
                        start_time_str: '2018-03-30',
                        end_time: '1521043200000',
                        end_time_str: '2018-04-30',
                        activity_pic_url: 'http://121.41.75.163:8000/source/image/20180322/15217002107217ve0.png',
                        activity_link: 'http://www.baidu.com',
                        isStart: true,
                        expire: '否'
                    },{
                        id: 2,
                        activity_type: '天天有礼',
                        start_time: '1519833600000',
                        start_time_str: '2018-03-30',
                        end_time: '1521043200000',
                        end_time_str: '2018-04-15',
                        activity_pic_url: 'http://121.41.75.163:8000/source/image/20180322/15217002107217ve0.png',
                        activity_link: 'http://www.baidu.com',
                        isStart: true,
                        expire: '是'
                    },{
                        id: 3,
                        activity_type: '其他',
                        start_time: '1519833600000',
                        start_time_str: '2018-02-30',
                        end_time: '1521043200000',
                        end_time_str: '2018-03-30',
                        activity_pic_url: 'http://121.41.75.163:8000/source/image/20180322/15217002107217ve0.png',
                        activity_link: 'http://www.baidu.com',
                        isStart: true,
                        expire: '是'
                    }],
                dialogFormVisible: false,
                form1: {
                    activity_type: '',
                    expire: '',
                    start_time: '',
                    end_time: '',
                    start_time_str: '',
                    end_time_str: '',
                    activity_pic_url: '',
                    activity_link: ''
                },
                fileList: [],
                limit: 20,
                count: 3,
                currentPage: 1,
                title1: ''
            }
        },
        components: {
            headTop
        },
        methods: {
//            重置
            resetForm(formName) {
                this.$refs[formName].resetFields('');
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
//            添加
            addact () {
                this.title1 = '新增活动'
                this.dialogFormVisible = true
                this.form1 = []
            },
//            修改
            modifyact (index, row) {
                this.title1 = '修改活动'
                this.dialogFormVisible = true
                this.form1 = Object.assign({}, row)
            },
//            删除行
            async handleDelete(index, row, data1) {
                this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    data1.splice(index, 1)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        }
    }
</script>

<style>
    .table_container{
        padding: 20px;
    }
</style>
